
let api = require('../../config/api')
let util = require('../../utils/util')

// pages/mine/mine.js 测试
Page({

    /**
     * 页面的初始数据
     */
    data: {
        tipLogin:false,
        userInfo:{trueName:"请登录"},
        showChooseAvatar:true
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let that = this;
        this.setData({"tipLogin":true});
        util.isLogin(function (){
            that.getUserInfo();
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        let that = this;
        if( !this.data.tipLogin ){
            util.isLogin(function (){
                that.getUserInfo();
            })
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {
        this.setData({"tipLogin":false});
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    onChooseAvatar(e) {
      let avatarUrl = e.detail.avatarUrl;
      let userInfo = this.data.userInfo;
      let that = this;
      //更新头像信息

      wx.uploadFile({
        filePath: avatarUrl,
        name: 'upfile',
        url: api.uploadFile,
        success:function (res){
          wx.hideLoading();
          let data = JSON.parse(res.data);
          util.request(api.modifyUserAvatarUrl, {"avatar":data.relaName}, 'POST').then(function (modifyRes){
            wx.hideLoading();
            if (modifyRes.code == 0){
              userInfo.avatar = modifyRes.data;
              that.setData({
                userInfo:userInfo
              })
            }else{
                wx.showToast({
                  title: modifyRes.message,
                  icon:"none"
                })
            }
        }).catch(function (e){
            wx.hideLoading();
            wx.showToast({
              title: e,
              icon:"none"
            })
        })
        },
        fail:function(e){
            wx.hideLoading();
            wx.showToast({
              title: e,
              icon:"none"
            })
        }
      })
    },
    goLogin(){
        let that = this;
        util.isLogin(function(){
            that.getUserInfo(); 
        });
    },
    onProfileClick(){
        util.isLogin(function (){
            wx.navigateTo({
                url: './profile/profile',
              })
        })

    },
    getrealtimephonenumber (e) {
      console.log(e.detail.code)  // 动态令牌
      console.log(e.detail.errMsg) // 回调信息（成功失败都会返回）
      console.log(e.detail.errno)  // 错误码（失败时返回）
      let that = this;
      wx.showLoading();
      util.request(api.getPhoneNumber+e.detail.code, {}, 'POST').then(function (res){
        wx.hideLoading();
        if (res.code == 0){
            let data = res.data;
            let userInfo = that.data.userInfo;
            userInfo.mobile_verify_status = '0';
            userInfo.mobile = data;
            that.setData({userInfo:userInfo})
        }else{
            wx.showToast({
              title: res.message,
              icon:"none"
            })
        }
    }).catch(function (e){
        wx.hideLoading();
        wx.showToast({
          title: e,
          icon:"none"
        })
    })
    },
    getUserInfo(){
        let that = this;
        wx.showLoading();

        util.request(api.getUserInfo, {}, 'POST').then(function (res){
            wx.hideLoading();
            if (res.code == 0){
                let data = res.data;
                if( data.nickname && data.nickname.length > 5 ){
                  data.nickname = data.nickname.substring(0,5);
                }
                let showChooseAvatar = true;
                if( data.avatar.indexOf("default_Avatar.png") != -1 ){
                  //默认头像
                  showChooseAvatar = true;
                }else {
                  showChooseAvatar = false;
                }
                that.setData(
                  {
                    userInfo:res.data,
                    showChooseAvatar:showChooseAvatar
                  }
                  )
            }else{
                wx.showToast({
                  title: res.message,
                  icon:"none"
                })
            }
        }).catch(function (e){
            wx.hideLoading();
            wx.showToast({
              title: e,
              icon:"none"
            })
        })
    },

    /**
     * 打开会话记录
     */
    goChatHistory() {
      wx.navigateTo({
        url: './chatHistory/chatHistory',
      })
    }
})